package com.wandafilm.app.jetpack.frame.ui.main

import android.text.TextUtils
import androidx.lifecycle.viewModelScope
import com.kotlin.android.api.config.ApiConfig
import com.kotlin.android.api.config.AppConfig
import com.kotlin.android.api.CallResult
import com.kotlin.android.api.RetrofitManager
import com.kotlin.android.api.bean.GatewayInfo
import com.kotlin.android.api.config.Env
import com.kotlin.android.api.key.ApiKey
import com.kotlin.android.core.BaseViewModel
import com.kotlin.android.ktx.ext.protocolUrl
import kotlinx.coroutines.launch

/**
 *
 * Created on 2020/5/7.
 *
 * @author o.s
 */
class MainViewModel : BaseViewModel() {
    private val api by lazy { RetrofitManager.api() }

    fun initGateway() {
        viewModelScope.launch(main) {
            CallResult<GatewayInfo>().success {
                it.localDnsInfo.apply {
                    Env.instance.apply {
                        when (AppConfig.instance.api) {
                            ApiKey.PRD -> {
                                hostMisc = safeUrl(miscApiHost, ApiConfig.HOST_ARRAY_PRD[0])
                                hostCinema = safeUrl(cinemaApiHost, ApiConfig.HOST_ARRAY_PRD[1])
                                hostUser = safeUrl(userApiHost, ApiConfig.HOST_ARRAY_PRD[2])
                                hostTicket = safeUrl(ticketApiHost, ApiConfig.HOST_ARRAY_PRD[3])
                                hostPayment = safeUrl(paymentApiHost, ApiConfig.HOST_ARRAY_PRD[4])
                                hostCard = safeUrl(cardApiHost, ApiConfig.HOST_ARRAY_PRD[5])
                                hostSnack = safeUrl(snackApiHost, ApiConfig.HOST_ARRAY_PRD[6])
                                hostCoupon = safeUrl(couponApiHost, ApiConfig.HOST_ARRAY_PRD[7])
                                hostActivity = safeUrl(activityApiHost, ApiConfig.HOST_ARRAY_PRD[8])
                                hostMkt = safeUrl(activityMktApiHost, ApiConfig.HOST_ARRAY_PRD[9])
                                hostCount = safeUrl(countApiHost, ApiConfig.HOST_ARRAY_PRD[10])
                            }
                            ApiKey.STG -> {
                                hostMisc = safeUrl(miscApiHost, ApiConfig.HOST_ARRAY_STG[0])
                                hostCinema = safeUrl(cinemaApiHost, ApiConfig.HOST_ARRAY_STG[1])
                                hostUser = safeUrl(userApiHost, ApiConfig.HOST_ARRAY_STG[2])
                                hostTicket = safeUrl(ticketApiHost, ApiConfig.HOST_ARRAY_STG[3])
                                hostPayment = safeUrl(paymentApiHost, ApiConfig.HOST_ARRAY_STG[4])
                                hostCard = safeUrl(cardApiHost, ApiConfig.HOST_ARRAY_STG[5])
                                hostSnack = safeUrl(snackApiHost, ApiConfig.HOST_ARRAY_STG[6])
                                hostCoupon = safeUrl(couponApiHost, ApiConfig.HOST_ARRAY_STG[7])
                                hostActivity = safeUrl(activityApiHost, ApiConfig.HOST_ARRAY_STG[8])
                                hostMkt = safeUrl(activityMktApiHost, ApiConfig.HOST_ARRAY_STG[9])
                                hostCount = safeUrl(countApiHost, ApiConfig.HOST_ARRAY_STG[10])
                            }
                            else -> {
                                hostMisc = safeUrl(miscApiHost, ApiConfig.HOST_ARRAY_QAS[0])
                                hostCinema = safeUrl(cinemaApiHost, ApiConfig.HOST_ARRAY_QAS[1])
                                hostUser = safeUrl(userApiHost, ApiConfig.HOST_ARRAY_QAS[2])
                                hostTicket = safeUrl(ticketApiHost, ApiConfig.HOST_ARRAY_QAS[3])
                                hostPayment = safeUrl(paymentApiHost, ApiConfig.HOST_ARRAY_QAS[4])
                                hostCard = safeUrl(cardApiHost, ApiConfig.HOST_ARRAY_QAS[5])
                                hostSnack = safeUrl(snackApiHost, ApiConfig.HOST_ARRAY_QAS[6])
                                hostCoupon = safeUrl(couponApiHost, ApiConfig.HOST_ARRAY_QAS[7])
                                hostActivity = safeUrl(activityApiHost, ApiConfig.HOST_ARRAY_QAS[8])
                                hostMkt = safeUrl(activityMktApiHost, ApiConfig.HOST_ARRAY_QAS[9])
                                hostCount = safeUrl(countApiHost, ApiConfig.HOST_ARRAY_QAS[10])
                            }
                        }
                    }
                }
            }.call (errorMessage = ApiConfig.GATEWAY_ERROR) {
                api.getGateway()
            }
        }
    }

    private fun safeUrl(url: String?, originUrl: String): String {
        return if (url != null && !TextUtils.isEmpty(url)) {
            url.protocolUrl(Env.isRelease())
        } else {
            originUrl
        }
    }
}